package com.hnust.controller.paper.component;

import com.hnust.domain.QuestionType;
import com.hnust.domain.Result;
import com.hnust.fx.FXMLController;
import com.hnust.service.TestPaperService;
import com.hnust.store.AutoPaperDataStore;
import com.hnust.store.DataStore;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.AnchorPane;
import org.springframework.beans.factory.annotation.Autowired;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

@FXMLController
public class AddAutoPaperKindController  implements Initializable {
    @Autowired
    private TestPaperService testPaperService;
    @Autowired
    private DataStore dataStore;
    @Autowired
    private AutoPaperDataStore autoPaperDataStore;
    @FXML
    public ComboBox comb;
    @FXML
    public AnchorPane container;

    private ObservableList<String> list= FXCollections.observableArrayList();
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        setComb();
        container.parentProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue!=null){
                setCombKind();
            }else{
                close();
            }
        });
    }

    //获取课程所对应的题目类型
    public void setCombKind() {
        testPaperService.getCourseQuesList(new Callback<Result<List<QuestionType>>>() {
            @Override
            public void onResponse(Call<Result<List<QuestionType>>> call, Response<Result<List<QuestionType>>> response) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        list.clear();
                        autoPaperDataStore.setQuestionTypes(response.body().getData());
                        response.body().getData().forEach(questionType -> {
                            list.add(questionType.getName());
                        });
                        setCombItem();
                    }
                });
            }

            @Override
            public void onFailure(Call<Result<List<QuestionType>>> call, Throwable t) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        Alert alert = new Alert(Alert.AlertType.ERROR, "请求失败，请检查网络设置！");
                        alert.showAndWait();
                    }
                });
            }
        },  dataStore.getToken(), autoPaperDataStore.getCourseId());
    }

    //设置combox中的属性
    public void setComb(){
        comb.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue observable, Object oldValue, Object newValue) {
                autoPaperDataStore.setKind((String)newValue);
            }
        });
    }

    //设置combox中数据
    public void setCombItem(){
        if (!list.isEmpty()){
            comb.getItems().addAll(list);
        }else{
            comb.getItems().add("网络异常");
        }
        comb.getSelectionModel().select(0);
    }

    //关闭，清空数据
    public void close(){
        comb.getItems().clear();
    }
}
